<?php
declare (strict_types=1);

namespace app\api\model\v1;

use think\Model;

/**
 * @mixin \think\Model
 */
class Exam extends AppModel
{
    const IS_STATUS_ZERO = 0; //关闭
    const IS_STATUS_ONE = 1; //开启

    const IS_EXAM_TYPE_ONE = 1; //固定试卷
    const IS_EXAM_TYPE_TWO = 2; //定时试卷

    //exam_creation_type
    const EXAM_CREATION_TYPE_ZERO = 0;
    const EXAM_CREATION_TYPE_ONE = 1;
    const EXAM_CREATION_TYPE_TWO = 2;
    const EXAM_CREATION_TYPE_THREE = 3;

    //is_release
    const IS_RELEASE_ZERO = 0;
    const IS_RELEASE_ONE = 1;

    public static $is_exam_type = array(
        self::IS_EXAM_TYPE_ONE => '固定试卷',
        self::IS_EXAM_TYPE_TWO => '定时试卷'
    );

    public static $is_status = array(
        self::IS_STATUS_ZERO => '关闭',
        self::IS_STATUS_ONE => '开启'
    );
    public static $is_release = array(
        self::IS_RELEASE_ZERO => '待发布',
        self::IS_RELEASE_ONE => '已发布'
    );

    public static $exam_creation_type = array(
        self::EXAM_CREATION_TYPE_ZERO => '未知',
        self::EXAM_CREATION_TYPE_ONE => '人工组题',
        self::EXAM_CREATION_TYPE_TWO => '随机生成',
        self::EXAM_CREATION_TYPE_THREE => '一次生成全部'
    );


    public function admin(){
        return $this->belongsTo(Admins::class,'create_id');
    }

    public function examStairType()
    {
        return $this->hasMany(ExamStairType::class, 'exam_id');
    }

    public function examSecondLevel()
    {
        return $this->hasMany(ExamSecondLevel::class, 'exam_id');
    }

    public function examDetail()
    {
        return $this->hasMany(ExamDetail::class, 'exam_id');
    }


    public function unit()
    {
        return $this->belongsTo(Unit::class, 'unit_id');
    }


    public function industry()
    {
        return $this->belongsTo(Industry::class, 'industry_id');
    }


    public function template()
    {
        return $this->belongsTo(Template::class, 'exam_template_id');
    }


    public function examAudit(){
        return $this->hasMany(AuditExam::class,'exam_id');
    }


    public function specialAuditUser(){
        return $this->hasMany(SpecialAuditUser::class,'exam_id');
    }


    /**
     * @param $disposeData
     * @param $exam_id
     * @param $create_id
     * @User: 刘海龙
     * @Date: 2021/9/27
     * @Time: 17:48
     * @return int
     * 同步数据
     */
    public static function synchronousData($disposeData, $exam_id, $create_id)
    {
        if (!empty($disposeData['data'])) {
            foreach ($disposeData['data'] as $key => $v) {
                $exam_stair_type_id = ExamStairType::create([
                    'exam_stair_title' => $v['template_type_title'],
                    'year' => $v['year'],
                    'exam_id' => $exam_id,
                    'create_id' => $create_id,
                ])->id;
                if (isset($v['templateSecondLevel'])) {
                    foreach ($v['templateSecondLevel'] as $child_key => $child_v) {
                        $exam_second_level_id = ExamSecondLevel::create([
                            'exam_second_title' => $child_v['second_level_title'],
                            'year' => $child_v['year'],
                            'exam_id' => $exam_id,
                            'exam_stair_type_id' => $exam_stair_type_id,
                            'create_id' => $create_id,
                        ])->id;
                        if (isset($child_v['templateDetail'])) {
                            foreach ($child_v['templateDetail'] as $child_child_key => $child_child_v) {
                                ExamDetail::create([
                                    'exam_detail_title' => $child_child_v['template_detail_title'],
                                    'year' => $child_child_v['year'],
                                    'desc' => $child_child_v['desc'],
                                    'deduct_marks_standard' => $child_child_v['deduct_marks_standard'],
                                    'once_deduct_marks' => $child_child_v['once_deduct_marks'],
                                    'most_deduct_marks' => $child_child_v['most_deduct_marks'],
                                    'exam_id' => $exam_id,
                                    'exam_stair_type_id' => $exam_stair_type_id,
                                    'exam_second_level_id' => $exam_second_level_id,
                                    'create_id' => $create_id,
                                ]);
                            }
                        }
                    }
                }
            }
            $result = 200;
        } else {
            $result = 403;
        }
        return $result;
    }
}
